package problem;

import datastruct.map.Map;

import java.util.*;

/**
 * @author RunningShrimp
 * @date 2021/5/18  20:42
 * @see <a href="https://leetcode-cn.com/problems/intersection-of-two-arrays/">349. 两个数组的交集</a>
 */
public class TwoArrayIntersection {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> intersectionSet = new HashSet<>(nums1.length);
        for (int i : nums1) {
            intersectionSet.add(i);
        }
        List<Integer> list = new ArrayList<>(intersectionSet.size());
        for (int i : nums2) {
            if (intersectionSet.contains(i)) {
                list.add(i);
                intersectionSet.remove(i);
            }
        }
        int[] array = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }
        return array;
    }

}
